package com.godtree.dboperation;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintStream;

/*
 备份MySQL数据库的命令     
 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql    


 备份MySQL数据库为带删除表的格式     
 备份MySQL数据库为带删除表的格式，能够让该备份覆盖已有数据库而不需要手动删除原有数据库。    

 mysqldump -–add-drop-table -uusername -ppassword databasename > backupfile.sql    


 直接将MySQL数据库压缩备份     

 mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz    


 备份MySQL数据库某个(些)表     

 mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql    


 同时备份多个MySQL数据库     

 mysqldump -hhostname -uusername -ppassword –databases databasename1 databasename2 databasename3 > multibackupfile.sql    


 仅仅备份数据库结构     

 mysqldump –no-data –databases databasename1 databasename2 databasename3 > structurebackupfile.sql    


 备份服务器上所有数据库     

 mysqldump –all-databases > allbackupfile.sql    


 还原MySQL数据库的命令     

 mysql -hhostname -uusername -ppassword databasename < backupfile.sql    


 还原压缩的MySQL数据库     

 gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename    


 将数据库转移到新服务器     

 mysqldump -uusername -ppassword databasename | mysql –host=*.*.*.* -C databasename   

 */
/**
 * 
* @ClassName: Backup2 
* @Description: 这个类是没用到的,是测试的时候产生的,用来执行sh文件,备份数据库
* @author gao
* @date 2014年8月30日 下午10:43:46 
* 这个类是独立的,是最原始测试用的
 */
public class Backup2 {
	private static String file = System.getProperty("user.dir")+"/WebContent/Initialization.sql";
	//追加字符串到文件中
	private static void saveAsFileWriter(String content) throws Exception{
		String temp=content+"\n";
    FileOutputStream fos = new FileOutputStream(file,true);//true表示在文件末尾追加  
    fos.write(temp.getBytes());
    fos.close();//流要及时关闭
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
            FileOutputStream out=new FileOutputStream(file);
            PrintStream p=new PrintStream(out);
            p.print("");
    } catch (FileNotFoundException e){
            e.printStackTrace();
        }
		try {
			Runtime rt = Runtime.getRuntime();
			String command = "mysqldump -h localhost -uroot -phideapple sportAlliance"; // 一定要加
																								// -h
																								// localhost(或是服务器IP地址)
			Process process = rt.exec(command);
			InputStreamReader isr = new InputStreamReader(
					process.getErrorStream());
			LineNumberReader input = new LineNumberReader(isr);
			String line;
			
			while ((line = input.readLine()) != null) {
				System.err.println("mysql报错:\n"+line);
				return;
			}
			
			InputStreamReader isr2 = new InputStreamReader(
					process.getInputStream());
			LineNumberReader input2 = new LineNumberReader(isr2);
			String line2;
			while ((line2 = input2.readLine()) != null) {

					try {
						saveAsFileWriter(line2);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
			System.out.println("备份成功!");
		} catch (IOException e) {
			System.out.println("备份失败!");
		}
	    System.out.println("===========os.name:"+System.getProperties().getProperty("os.name"));  
	    System.out.println("===========file.separator:"+System.getProperties().getProperty("file.separator"));  
	    
	}

}
